from rest_framework import serializers

from users.models import User
from .models import Spit, SpitComment, SpitLike


class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ["id", "username"]


class SpitListSerializer(serializers.ModelSerializer):
    """吐槽列表序列化器"""

    class Meta:
        model = Spit
        fields = ["id", "user_id", "like_counts", "create_time", "content", "is_collection", ]
        # read_only_fields = ["id", "like_counts"]


class SpitCommentSerializer(serializers.ModelSerializer):
    """评论吐槽序列化器"""
    user = serializers.StringRelatedField(label="用户名")

    class Meta:
        model = SpitComment
        fields = ["id", "spit_id", "user_id", "content", "user",
                  "like_counts"]

    def validate_content(self,value):
        if value is None:
            return serializers.ValidationError("评论内容不能为空")
        return value

    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class SubSpitCommentSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(label="用户名")
    parent = SpitCommentSerializer(many=True, read_only=True)

    class Meta:
        model = SpitComment
        fields = ["id", "spit_id", "user_id", "user", "like_counts",
                  "content", "parent"]

    def validate_content(self, value):
        if value is None:
            return serializers.ValidationError("评论内容不能为空")
        return value

    def create(self, validated_data):
        validated_data['user'] = self.context['request'].user
        return super().create(validated_data)


class SpitDetailSerializer(serializers.ModelSerializer):
    """吐槽详情序列化器"""
    # username = UserSerializer()
    user = serializers.StringRelatedField(label="用户名")
    comments = SpitCommentSerializer(many=True, read_only=True)

    class Meta:
        model = Spit
        fields = ["id", "user_id", "content", "comment_counts", "user",
                  "share_counts", "is_collection", "like_counts", "comments"]
        read_only_fields = ["id", "comment_counts", "share_counts",
                            "like_counts"]


class SpitReleaseSerializer(serializers.ModelSerializer):
    """发布吐槽序列化器"""
    username = UserSerializer()
    # user = serializers.StringRelatedField(label="用户名")
    # allow_anonymity = serializers.CharField(label="选取匿名", write_only=True)
    # content = serializers.StringRelatedField(label="内容")

    class Meta:
        model = Spit
        # fields = ["id", "username", "contents", "allow_anonymity"]
        fields = ["user_id", "username", "content"]
        extra_kwargs = {
            "content": {
                "min_length": 1,
                "max_length": 140,
                "error_messages": {
                    "min_length": "仅允许1-140个字符的内容",
                    "max_length": "仅允许1-140个字符的内容",
                }
            },
        }

    # def validate_allow_anonymity(self, allow_anonymity):
    #     """验证是否匿名"""
    #     user = self.context['request'].user
    #     if allow_anonymity == "true":
    #         user.username = "匿名用户"
    #     return allow_anonymity, user
    #

    # def validate_content(self, value):
    #     if value is None:
    #         return serializers.ValidationError("发布内容不能为空")
    #     return value
    #
    # def create(self, validated_data):
    #
    #     # del validated_data["allow_anonymity"]
    #
    #     user = self.context['request'].user
    #     content = validated_data["content"]
    #     # spit = super().create(validated_data)
    #     spit = Spit.objects.create(content=content, user_id=user.id)
    #     spit.save()
    #     return spit


class SpitLikeSerializer(serializers.ModelSerializer):
    class Meta:
        model = SpitLike
        fields = "__all__"





